package other

import (
	"errors"
	"gitee.com/gopher2011/go-zero-gva/internal/svc"
	"gitee.com/gopher2011/go-zero-gva/internal/types"
	"gorm.io/gorm"
	"time"
)

func (l *Other) JsonInBlacklist(req *svc.JwtBlacklist) (*types.Response, error) {
	if err := l.SvcCtx.DB.Create(req).Error;err != nil {
		return &types.Response{
			Code: 7,
			Msg: "jwt作废失败",
		}, err
	}
	return &types.Response{
		Code: 0,
		Msg: "jwt作废成功",
	}, nil
}

// 判断JWT是否在黑名单内部
func (l *Other) IsBlacklist(jwt string) bool {
	return !errors.Is(l.SvcCtx.DB.Where("jwt = ?",jwt).First(&svc.JwtBlacklist{}).Error,gorm.ErrRecordNotFound)
}

func (l *Other) GetRedisJWT(userName string) (err error, redisJWT string) {
	err = l.SvcCtx.Cache.Get(userName, &redisJWT)
	return
}

func (l *Other) SetRedisJWT(jwt string, userName string) (err error) {
	return l.SvcCtx.Cache.SetWithExpire(userName,jwt,time.Duration(l.SvcCtx.Config.Auth.AccessExpire)*time.Second)
}
